// https://www.luogu.com.cn/problem/P2858
#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
const int max_size = 5 + 100;
const int maxn = 5 + 2000;

int n;
int a[maxn];
int f[maxn][maxn];  // f[l][r] 是 [l, r] 贩卖的最大值
/*
    最优子结构： 显然
    f[l][r] 是第 n - (r - l) 天卖掉端点物品
*/

int dfs(int l, int r, int dep = 1) {
    if (l == r) return a[l] * dep;
    if (f[l][r]) return f[l][r];
    return f[l][r] = max(dfs(l + 1, r, dep + 1) + a[l] * dep,
                         dfs(l, r - 1, dep + 1) + a[r] * dep);
}

int main() {
    scanf("%d", &n);
    rf(i, n) scanf("%d", a + i);
    // printf("%d", dfs(1, n));
    for (int siz = 1; siz <= n; ++siz) {
        for (int l = 1, r = siz; r <= n; ++l, ++r) {
            int dep = l + n - r;
            f[l][r] = max(f[l + 1][r] + dep * a[l], f[l][r - 1] + dep * a[r]);
        }
    }
    printf("%d", f[1][n]);
}
